﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Xml;
using YArchitech.LIB;

namespace HYCreateDuct.MethodHelp
{
	internal class XmlHelp
	{
		public XmlHelp()
		{
			if (!File.Exists(this.path))
			{
				File.Copy(Path.Combine(Product.DataLocation, "User\\Duct_SystemTypeSet.xml"), this.path);
			}
			if ((XMLOperating.LoadXml(this.path).FirstChild.NextSibling as XmlElement).GetAttribute("version") != "BIMSpace2019")
			{
				File.Copy(Path.Combine(Product.DataLocation, "User\\Duct_SystemTypeSet.xml"), this.path, true);
			}
		}

		public List<DuctSystemList> ReadSystemSet(string pathAll = "")
		{
			List<DuctSystemList> list = new List<DuctSystemList>();
			List<DuctSystemList> result;
			try
			{
				if (string.IsNullOrEmpty(pathAll))
				{
					pathAll = this.path;
				}
				string xpath = "systemTypeManage";
				XmlDocument xmlDocument = new XmlDocument();
				xmlDocument.Load(pathAll);
				foreach (object obj in xmlDocument.SelectSingleNode(xpath).ChildNodes)
				{
					XmlNode xmlNode = (XmlNode)obj;
					XmlElement xmlElement = (XmlElement)xmlNode;
					string value = xmlElement.Attributes["name"].Value;
					string value2 = xmlElement.Attributes["Type"].Value;
					DuctSystemList ductSystemList = new DuctSystemList();
					ductSystemList.strTypeName = value;
					ductSystemList.strId = value2;
					List<DuctSytemSet> list2 = new List<DuctSytemSet>();
					foreach (object obj2 in xmlNode.ChildNodes)
					{
						XmlNode xmlNode2 = (XmlNode)obj2;
						if (((XmlElement)xmlNode2).Attributes["name"].Value.Equals("风管系统"))
						{
							foreach (object obj3 in xmlNode2.ChildNodes)
							{
								XmlElement xmlElement2 = (XmlElement)((XmlNode)obj3);
								list2.Add(new DuctSytemSet
								{
									strSystemName = xmlElement2.Attributes["pipingSystemType"].Value,
									strSystemTypeAbb = xmlElement2.Attributes["PipingSystemTypeAbb"].Value,
									strLineColor = xmlElement2.Attributes["color"].Value,
									strLineType = xmlElement2.Attributes["linePattern"].Value,
									strLineWidth = xmlElement2.Attributes["lineWeight"].Value,
									strSystemType = xmlElement2.Attributes["SystemType"].Value,
									strPresslevel = xmlElement2.Attributes["PressLevel"].Value,
									strMateria = xmlElement2.Attributes["Materia"].Value
								});
							}
						}
					}
					ductSystemList.lstSet = list2;
					list.Add(ductSystemList);
				}
				result = list;
			}
			catch
			{
				result = list;
			}
			return result;
		}

		public void WriteXml(string dir, string TypeName, string id, List<DuctSytemSet> lstSet)
		{
			XmlDocument xmlDocument = new XmlDocument();
			XmlDeclaration newChild = xmlDocument.CreateXmlDeclaration("1.0", "UTF-8", null);
			xmlDocument.AppendChild(newChild);
			XmlElement xmlElement = xmlDocument.CreateElement("systemTypeManage");
			xmlElement.SetAttribute("version", "BIMSpace2019");
			xmlDocument.AppendChild(xmlElement);
			if (lstSet.Count > 0)
			{
				XmlElement xmlElement2 = xmlDocument.CreateElement("styleNode");
				xmlElement2.SetAttribute("name", TypeName);
				xmlElement2.SetAttribute("Type", id);
				xmlElement.AppendChild(xmlElement2);
				XmlElement xmlElement3 = xmlDocument.CreateElement("System");
				xmlElement3.SetAttribute("name", "风管系统");
				xmlElement3.SetAttribute("Type", id);
				xmlElement2.AppendChild(xmlElement3);
				foreach (DuctSytemSet ductSytemSet in lstSet)
				{
					XmlElement xmlElement4 = xmlDocument.CreateElement("add");
					xmlElement4.SetAttribute("pipingSystemType", ductSytemSet.strSystemName);
					xmlElement4.SetAttribute("PipingSystemTypeAbb", ductSytemSet.strSystemTypeAbb);
					xmlElement4.SetAttribute("color", ductSytemSet.strLineColor);
					xmlElement4.SetAttribute("linePattern", ductSytemSet.strLineType);
					xmlElement4.SetAttribute("lineWeight", ductSytemSet.strLineWidth);
					xmlElement4.SetAttribute("SystemType", ductSytemSet.strSystemType);
					xmlElement4.SetAttribute("PressLevel", ductSytemSet.strPresslevel);
					xmlElement4.SetAttribute("Materia", ductSytemSet.strMateria);
					xmlElement3.AppendChild(xmlElement4);
				}
			}
			xmlDocument.Save(dir);
		}

		public void WriteXml(List<string> lst, List<DuctSystemList> lstDU)
		{
			if (File.Exists(this.path))
			{
				File.Delete(this.path);
			}
			XmlDocument xmlDocument = new XmlDocument();
			XmlDeclaration newChild = xmlDocument.CreateXmlDeclaration("1.0", "UTF-8", null);
			xmlDocument.AppendChild(newChild);
			XmlElement xmlElement = xmlDocument.CreateElement("systemTypeManage");
			xmlElement.SetAttribute("version", "BIMSpace2019");
			xmlDocument.AppendChild(xmlElement);
			int num = 0;
			foreach (DuctSystemList ductSystemList in lstDU)
			{
				XmlElement xmlElement2 = xmlDocument.CreateElement("styleNode");
				xmlElement2.SetAttribute("name", lst[num]);
				num++;
				xmlElement2.SetAttribute("Type", ductSystemList.strId);
				xmlElement.AppendChild(xmlElement2);
				XmlElement xmlElement3 = xmlDocument.CreateElement("System");
				xmlElement3.SetAttribute("name", "风管系统");
				xmlElement3.SetAttribute("Type", ductSystemList.strId);
				xmlElement2.AppendChild(xmlElement3);
				foreach (DuctSytemSet ductSytemSet in ductSystemList.lstSet)
				{
					XmlElement xmlElement4 = xmlDocument.CreateElement("add");
					xmlElement4.SetAttribute("pipingSystemType", ductSytemSet.strSystemName);
					xmlElement4.SetAttribute("PipingSystemTypeAbb", ductSytemSet.strSystemTypeAbb);
					xmlElement4.SetAttribute("color", ductSytemSet.strLineColor);
					xmlElement4.SetAttribute("linePattern", ductSytemSet.strLineType);
					xmlElement4.SetAttribute("lineWeight", ductSytemSet.strLineWidth);
					xmlElement4.SetAttribute("SystemType", ductSytemSet.strSystemType);
					xmlElement4.SetAttribute("PressLevel", ductSytemSet.strPresslevel);
					xmlElement4.SetAttribute("Materia", ductSytemSet.strMateria);
					xmlElement3.AppendChild(xmlElement4);
				}
			}
			xmlDocument.Save(this.path);
		}

		private string path = Path.Combine(Product.UserDataLocation, "Duct_SystemTypeSet.xml");
	}
}
